Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing.Printing
Imports DevExpress.XtraReports.Localization
Imports DevExpress.XtraReports.UserDesigner
Imports System.ComponentModel.Design
Imports System.Diagnostics
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
Imports System.IO
Imports System.Text

Public Class Report_Member

    ' Public ds As New DataSet
    Dim intReportID As Integer
    Dim ReportLocation As String
    Dim rpt As New XtraReport
    Dim arrPic() As Byte
    Public ReportCategory As Integer = 6
    Private mdiController As XRDesignMdiController

    Private Sub mdiController_DesignPanelLoaded(ByVal sender As Object, _
ByVal e As DesignerLoadedEventArgs)
        Dim panel As XRDesignPanel = CType(sender, XRDesignPanel)
        mdiController.AddCommandHandler(New SaveCommandHandler(panel))
    End Sub

#Region " Overridable  OnReportStateChanged "
    Public Overridable Sub OnReportStateChanged(ByVal sender As Object, ByVal e As ReportStateEventArgs)
        'If e.ReportState = ReportState.Opened Then

        '    'MyReportID = intReportID

        '    Dim serviceProvider As IServiceProvider = CType(sender, IServiceProvider)

        '    Dim menuCommandService As IMenuCommandService = CType(serviceProvider.GetService(GetType(IMenuCommandService)), IMenuCommandService)
        '    Dim fileSaveAsCommand As MenuCommand = menuCommandService.FindCommand(DevExpress.XtraReports.UserDesigner.Native.UICommands.SaveFileAs)

        '    'menuCommandService.RemoveCommand(fileSaveAsCommand)
        '    'menuCommandService.AddCommand(New MenuCommand(AddressOf OnSaveFileAs, DevExpress.XtraReports.UserDesigner.Native.UICommands.SaveFileAs))

        '    Dim fileSaveCommand As MenuCommand = menuCommandService.FindCommand(DevExpress.XtraReports.UserDesigner.Native.UICommands.SaveFile)
        '    menuCommandService.RemoveCommand(fileSaveCommand)
        '    menuCommandService.AddCommand(New MenuCommand(AddressOf OnSaveFile, DevExpress.XtraReports.UserDesigner.Native.UICommands.SaveFile))

        '    Dim fileOpenCommand As MenuCommand = menuCommandService.FindCommand(DevExpress.XtraReports.UserDesigner.Native.UICommands.OpenFile)
        '    menuCommandService.RemoveCommand(fileOpenCommand)


        '    'menuCommandService.AddCommand(New MenuCommand(AddressOf OnOpenFile, DevExpress.XtraReports.UserDesigner.Native.UICommands.OpenFile))
        'End If
    End Sub
#End Region

    Private Sub BarBtnReportDesigner_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarBtnReportDesigner.ItemClick
        Dim form As New XRDesignForm()
        mdiController = form.DesignMdiController

        ' Handle the DesignPanelLoaded event of the MDI controller,
        ' to override the SaveCommandHandler for every loaded report.
        AddHandler mdiController.DesignPanelLoaded, AddressOf mdiController_DesignPanelLoaded

        ' Open an empty report in the form.
        mdiController.OpenReport(rpt)

        ' Show the form.
        form.ShowDialog()
        mdiController.ActiveDesignPanel.CloseReport()

    End Sub

    Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadIcon(Me)
        Dim str As String
        str = "SELECT * FROM Reports WHERE Category = " & ReportCategory
        LoadDataToLookUpEdit(str, "ID", "ReportName", "Report Name", Me.cboReportName)
        'Get Default Report
        str = "SELECT ID FROM Reports WHERE Category = " & ReportCategory
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim intDefaultReportID As Integer
        If Not cmd.ExecuteScalar Is Nothing Then
            intDefaultReportID = cmd.ExecuteScalar
            Me.cboReportName.EditValue = intDefaultReportID
        End If



    End Sub


    Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
        If Me.cboReportName.EditValue Is Nothing Then
            Exit Sub
        End If

        intReportID = Me.cboReportName.EditValue
        Dim str As String = "SELECT * FROM Reports WHERE ID = " & Me.cboReportName.EditValue
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader
        While dr.Read
            arrPic = dr.Item("ReportFile")
        End While
        dr.Close()

        Dim ds As New DataSet
        'Total Sales
        str = "SELECT * FROM People_View "
        cmd.CommandText = str
        Dim da As New SqlDataAdapter(cmd)
        da.Fill(ds, "Member")

        Dim mmReport As New MemoryStream(arrPic)
        rpt = XtraReport.FromStream(mmReport, True)
        rpt.DataSource = ds
        PrintControl2.PrintingSystem = rpt.PrintingSystem
        rpt.CreateDocument()

    End Sub


    Sub OnSaveFile(ByVal sender As Object, ByVal e As EventArgs)

        Dim str As String
        str = "UPDATE Reports SET ReportFile = @ReportFile WHERE ID = " & intReportID
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Designer.SaveReport("C:\TempReport.repx")
        Dim br As New IO.BinaryReader(New IO.FileStream("C:\TempReport.repx", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read))
        Dim arr() As Byte = br.ReadBytes(br.BaseStream.Length)
        cmd.Parameters.Add("ReportFile", SqlDbType.Image).Value = arr
        If cmd.ExecuteNonQuery > 0 Then
            MsgBox("Report Saved.")
            br.Close()
            DeleteTempReport()
        End If

    End Sub

    Private Sub DeleteTempReport()
        Try
            If File.Exists("C:\TempReport.repx") = True Then
                File.Delete("C:\TempReport.repx")
            End If
        Catch ex As Exception

        End Try
    End Sub



End Class